package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Case;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.presenter.CasePresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.MaxTextLengthFilter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

import static com.luck.picture.lib.config.PictureConfig.CHOOSE_REQUEST;

/**
 * 上传病历界面
 * Created by wjy on 2019/1/5.
 */
public class UploadCaseActivity extends BaseActivity<CasePresenter> implements IUploadView<List<Case>> {

    @BindView(R.id.et_name)
    EditText mNameView;
    @BindViews({R.id.iv_card_1, R.id.iv_card_2, R.id.iv_card_3, R.id.iv_card_4, R.id.iv_card_5})
    List<ImageView> mCardViews;

    private List<String> list=new ArrayList<>();

    @Override
    public int getLayoutResource() {
        return R.layout.activity_upload_case;
    }

    @Override
    protected CasePresenter createPresenter() {
        return new CasePresenter(this);
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected void setupView() {
        super.setupView();
        mNameView.setFilters(new InputFilter[]{new MaxTextLengthFilter(20,this,"病例名称限20字以内")});
    }

    @Override
    protected void clickRightButton() {
        if(ConstantApp.isFastDoubleClick(1500)){
            return;
        }

        String name = mNameView.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showToast(this, "请输入病历名称");
        } else {
            if(list.size()<=0){
                ToastUtils.showToast(UploadCaseActivity.this,"至少上传一张病历照片");
                return;
            }
            uploadFile();


        }

    }

    @Override
    protected CharSequence getRightButtonText() {
        return "上传";
    }

    //身份证认证
    @OnClick(R.id.iv_card_5)
    public void identification() {
            if(list.size()>=5){
                ToastUtils.showToast(UploadCaseActivity.this,"最多选择5张照片");
                return;
            }
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(1)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
    }
    private void uploadFile() {
        imgSum=0;
        for (int i=0;i<list.size();i++) {
            mPresenter.uploadFile(list.get(i));
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia localMedia : selectList) {
                        list.add(localMedia.isCompressed() ? localMedia.getCompressPath() : localMedia.getPath());
                        mCardViews.get(list.size()-1).setVisibility(View.VISIBLE);
                        Glide.with(this).load(list.get(list.size()-1)).into(mCardViews.get(list.size()-1));
                        break;
                    }
                    break;
            }
        }
    }
    private int imgSum;
    private String mImageUrl;

    @Override
    public void onUploadSuccess(String path, String url) {
        imgSum++;
        mImageUrl="";
        for (int i=0;i<list.size();i++) {
            if (TextUtils.equals(list.get(i), path)) {
                list.set(i,url);
                break;
            }
        }

        for (int i=0;i<list.size();i++){
            if(i==0){
                mImageUrl=mImageUrl+list.get(0);
            }else {
                mImageUrl=mImageUrl+","+list.get(i);
            }
        }
        if(imgSum==list.size()){
            mPresenter.addCase(mNameView.getText().toString().trim(), mImageUrl);
        }

    }
    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<Case> cases) {
        ToastUtils.showToast(UploadCaseActivity.this,"上传成功");
        finish();
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }


    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
    }
}
